/*
 * CheckBoxMenuItemDelegate.cpp  0.4.0 / Dec 05, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "CheckBoxMenuItemDelegate.h"

#include "QApplicationDelegate.h"
#include "QIconAdapter.h"

// CheckBoxMenuItemDelegate :: Protected Constructor. --------------------------

/*!
 * \brief Constructs new CheckBoxMenuItemDelegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj the CheckBoxMenuItem java object
 */
CheckBoxMenuItemDelegate::CheckBoxMenuItemDelegate(JNIEnv *env, jobject obj)
  : ActionComponentDelegate(env, obj), m_shortcut(0)
{
  // empty constructor body
}

// CheckBoxMenuItemDelegate :: Native Calls. -----------------------------------

/*!
 * \brief Sets text.
 *
 * \param env the JNIEnv pointer
 * \param text the text
 */
void CheckBoxMenuItemDelegate::setText(JNIEnv *env, jstring text)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setText, SWING_ENV_ARG, JSTRING_ARG(text))
  if (isQWidget()) {
    checkBox()->setText(QStringAdapter(env, text));
  } else {
    action()->setText(QStringAdapter(env, text));
  }
}

/*!
 * \brief Sets short cut.
 *
 * \param env the JNIEnv pointer
 * \param shortcut the shortcut
 */
void CheckBoxMenuItemDelegate::setShortcut(JNIEnv *env, jstring shortcut)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setShortcut, SWING_ENV_ARG, JSTRING_ARG(shortcut))
  QStringAdapter shortcutString(env, shortcut);
  if (isQWidget()) {
    if (!m_shortcut) {
      m_shortcut = new QShortcut(widget());
    }
    if (shortcutString.isEmpty()) {
      m_shortcut->setKey(QKeySequence::UnknownKey);
    } else {
      m_shortcut->setKey(QKeySequence(shortcutString));
    }
  } else {
    if (shortcutString.isEmpty()) {
      action()->setShortcut(QKeySequence::UnknownKey);
    } else {
      action()->setShortcut(QKeySequence(shortcutString));
    }
  }
}

/*!
 * \brief Sets icon.
 *
 * \param env the JNIEnv pointer
 * \param iconSet the icon
 */
void CheckBoxMenuItemDelegate::setIcon(JNIEnv *env, jobject iconSet)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setIcon, SWING_ENV_ARG, JOBJECT_ARG(iconSet))
  if (isQWidget()) {
    if (iconSet) {
      checkBox()->setIcon(QIconAdapter(env, iconSet));
    } else {
      checkBox()->setIcon(QIcon());
    }
  } else {
    if (iconSet) {
      action()->setIcon(QIconAdapter(env, iconSet));
    } else {
      action()->setIcon(QIcon());
    }
  }
}

/*!
 * \brief Sets whether or not the item is checked.
 *
 * \param env the JNIEnv pointer
 * \param checked \c true if checked
 */
void CheckBoxMenuItemDelegate::setChecked(JNIEnv *env, jboolean checked)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setChecked, SWING_ENV_ARG, JBOOLEAN_ARG(checked))
  if (isQWidget()) {
    checkBox()->setChecked(checked);
  } else {
    action()->setChecked(checked);
  }
}

// CheckBoxMenuItemDelegate :: Static Native Calls. ----------------------------

/*!
 * \brief Initializes CheckBoxMenuItemDelegate class.
 *
 * \param env the JNIEnv pointer
 * \param cls the CheckBoxMenuItem java class
 *
 * \exception FatalError if failed
 */
void CheckBoxMenuItemDelegate::initialize(JNIEnv *env, jclass cls)
{
  Q_ASSERT(env);
  Q_ASSERT(cls);
  Q_ASSERT (!sm_checkedChangedMID);
  if (!INIT_UMID(checkedChanged, cls, "(IZ)V")) {
    FATAL_LOGGED_ERROR("CheckBoxMenuItemDelegate initialization failed");
  }
}

/*!
 * \brief Returns new instance of the delegate object
 *
 * \param env the JNIEnv pointer
 * \param obj the CheckBox java object
 * 
 * \return new instance of the delegate object
 */
jlong CheckBoxMenuItemDelegate::newInstance(JNIEnv *env, jobject obj)
{
  if (JNISwingWrapper::isSwingThread(env)) {
    return reinterpret_cast<jlong>(new CheckBoxMenuItemDelegate(env, obj));
  }
  return SwingUtilitiesDelegate::invokeLongAndWait(env, obj, createMID());
}

// CheckBoxMenuItemDelegate :: Native Callbacks. -------------------------------

/*!
 * \brief Calls \c checkedChanged method of the MenuItemDelegate object.
 *
 * \param modifiers the keyboard modifiers
 * \param checked \c true if the item is checked
 */
void CheckBoxMenuItemDelegate::checkedChanged(jint modifiers, jboolean checked)
{
  jniObject()->callVoidMethod(sm_checkedChangedMID, modifiers, checked);
}

// CheckBoxMenuItemDelegate :: Protected Member Functions. ---------------------

/*!
 * \brief Toggled slot.
 *
 * \param checked \c true if the item is checked
 */
void CheckBoxMenuItemDelegate::toggledSlot(bool checked)
{
  checkedChanged(QApplicationDelegate::keyboardModifiers(), checked);
}

/*!
 * \brief State changed slot.
 *
 * \param state the state
 */
void CheckBoxMenuItemDelegate::stateChangedSlot(int state)
{
  checkedChanged(QApplicationDelegate::keyboardModifiers(), state);
}

// CheckBoxMenuItemDelegate :: Overridden Member Functions. --------------------

/*!
 * \brief Creates QWidget adaptee.
 *
 * \return QWidget adaptee
 */
QWidget *CheckBoxMenuItemDelegate::createQWidget()
{
  QCheckBox *checkBox = new QCheckBox();
  connectDirect(checkBox, SIGNAL(stateChanged(int)),
                SLOT(stateChangedSlot(int)));
  return checkBox;
}

/*!
 * \brief Creates QAction adaptee.
 *
 * \return QAction adaptee
 */
QAction *CheckBoxMenuItemDelegate::createQAction()
{
  QAction *action = new QAction(this);
  action->setCheckable(true);
  connectDirect(action, SIGNAL(toggled(bool)), SLOT(toggledSlot(bool)));
  return action;
}

/*!
 * \inheritDoc
 */
void CheckBoxMenuItemDelegate::deleteQWidget(QWidget *widget)
{
  if (m_shortcut) {
    delete m_shortcut;
    m_shortcut = 0;
  }
  connectDirect(widget, SIGNAL(stateChanged(int)), SLOT(stateChangedSlot(int)));
  ActionComponentDelegate::deleteQWidget(widget);
}

/*!
 * \inheritDoc
 */
void CheckBoxMenuItemDelegate::deleteQAction(QAction *action)
{
  disconnect(action, SIGNAL(toggled(bool)), SLOT(toggledSlot(bool)));
  ActionComponentDelegate::deleteQAction(action);
}

/*!
 * \inheritDoc
 */
QWidget *CheckBoxMenuItemDelegate::convertToQWidget(QAction *action)
{
  QCheckBox *checkBox = static_cast<QCheckBox *>(
                             ActionComponentDelegate::convertToQWidget(action));
  checkBox->setText(action->text());
  if (m_shortcut) {
    delete m_shortcut;
  }
  m_shortcut = new QShortcut(checkBox);
  m_shortcut->setKey(action->shortcut());
  checkBox->setChecked(action->isChecked());
  checkBox->setIcon(action->icon());
  return checkBox;
}

/*!
 * \inheritDoc
 */
QAction *CheckBoxMenuItemDelegate::convertToQAction(QWidget *widget)
{
  QCheckBox *checkBox = static_cast<QCheckBox *>(widget);
  QAction *action = ActionComponentDelegate::convertToQAction(checkBox);
  action->setText(checkBox->text());
  if (m_shortcut) {
    action->setShortcut(m_shortcut->key());
  }
  action->setChecked(checkBox->isChecked());
  action->setIcon(checkBox->icon());
  return action;
}

// CheckBoxMenuItemDelegate :: Private Static Data Member. ---------------------

jmethodID CheckBoxMenuItemDelegate::sm_checkedChangedMID = 0;
